package com.jz.yanshe.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.BankCardResponse;
import com.jz.jd.rest.response.BankResponse;
import com.jz.jd.rest.response.BaseResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.com.feierman.R;

import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.ui.widget.CustomPopWindow;
import com.jz.yanshe.util.PreferencesHelper;

/**
 * Created by Administrator on 2018/3/9.
 */

public class AddBankActivity extends BaseActivity {

    private int type;
    private BankCardResponse.BankCard bankCard;
    private CustomPopWindow mListPopWindow;
    private List<BankResponse.Bank> list = new ArrayList<>();


    @BindView(R.id.tv_user_name)
    EditText tv_user_name;

    @BindView(R.id.tv_card_number)
    EditText tv_card_number;

    @BindView(R.id.tv_bank_name)
    TextView tv_bank_name;
    @BindView(R.id.tv_card_type)
    EditText tv_card_type;

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {
        bankCard = (BankCardResponse.BankCard) getIntent().getSerializableExtra("obj");
        if (bankCard == null) {
            type = 0;
        } else {
            type = 1;
            tv_user_name.setText(bankCard.bank_holder);
            tv_card_number.setText(bankCard.bank_num);
            tv_bank_name.setText(bankCard.bank_name);
            tv_card_type.setText(bankCard.bank_type);
        }
        getBankList();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_add_bank;
    }


    @OnClick(R.id.btn_add)
    void btn_add() {
        if (type == 0) {
            addBankCard();
        }else{
            updateBankCard();
        }
    }



    private void addBankCard() {
        RestProxy.getInstance().addBankCard(PreferencesHelper.getInstance().getString(AddBankActivity.this,"token"), tv_user_name.getText().toString(), tv_bank_name.getText().toString(), tv_card_number.getText().toString(), tv_card_type.getText().toString(),new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Toast.makeText(AddBankActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onError(Throwable e) {

                if (e instanceof BusinessException) {
                    Toast.makeText(AddBankActivity.this,((BusinessException)e).errorDescription,Toast.LENGTH_SHORT).show();
                toLogin(((BusinessException)e).errorCode);}
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void updateBankCard() {
        RestProxy.getInstance().updateBankCard(PreferencesHelper.getInstance().getString(AddBankActivity.this,"token"),tv_user_name.getText().toString(), tv_bank_name.getText().toString(), tv_card_number.getText().toString(), tv_card_type.getText().toString(),new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Toast.makeText(AddBankActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onError(Throwable e) {

                if (e instanceof BusinessException) {
                toLogin(((BusinessException)e).errorCode);}
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @OnClick(R.id.tv_bank_name)
    void tv_bank_name(){
        showPopListView();
    }


    private void showPopListView() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_list, null);
        //处理popWindow 显示内容
        handleListView(contentView);
        //创建并显示popWindow
        mListPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .size(400, 400) // 显示大小
                .create()
                .showAsDropDown(tv_bank_name, 0, 0);
    }

    private void handleListView(View contentView) {
        RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerView);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        MyAdapter adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyAdapter.Holder myHolder = new MyAdapter.Holder(LayoutInflater.from(AddBankActivity.this).inflate(R.layout.item_bank_info, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(final Holder holder, final int position) {
            holder.tv_bankname.setText(list.get(position).common_configuration_description);
            holder.tv_bankname.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tv_bank_name.setText(list.get(position).common_configuration_description);
                    mListPopWindow.dissmiss();
                }
            });
        }


        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_bankname;

            public Holder(View itemView) {
                super(itemView);
                tv_bankname = (TextView) itemView.findViewById(R.id.tv_bankname);
            }
        }
    }

    private void getBankList() {
        RestProxy.getInstance().getBank( "",new Observer<BankResponse>() {
            @Override
            public void onSubscribe(Disposable d) {
//                if (!NetworkUtil.isNetworkAvailable(getApplicationContext())) {
//                    Log.d(TAG, "Network NOT Available!");
//                    d.dispose();
//                }
            }

            @Override
            public void onNext(BankResponse bankResponse) {
                list.clear();
                list.addAll(bankResponse.list);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
